import {api} from '@/pages/product/detail/libs/api.js';
let _self;
export const index =  {
	props: {
		show:{type:Boolean,default:false},
		product:{type:Object,default:{}},
		formInfo:{type:Object,default:{}},
		orderItemId:{type:Number,default:0}
	},
	watch:{
	
		product(val){
			this.initData();
		},
		
		formData(val){
		},
		
		isConfirm(val){
			this.confirmSubmit(val);
		},
		
		
		salerInfo(val){
			if(val.user_code){
				this.customerData={"user_code":val.user_code};
			}	
		},
		 
		specsChange(val){
			if(val){
				for(let key in val){
					this.formData[key]=val[key];
				}
			}
		}
		
	
	},
	data() {
		return {
			host:process.env.HOST,
			errorField:"",
			showTip:false,
			isConfirm:false,
			showTxt:"",
			specsChange:{},
			formData:{"piece_num":0,"box_num":0,"roll_num":0,"price":0,"total":0,"specs_index":0,"box_price":0},
			host:process.env.HOST,
			selectList:{
				"thick":{"item":[],"index":0,"key":"value","data":"thick_list","name":"厚度","form_keys":{"thick":"name"}},
				"pro_zg":{"item":[],"index":0,"key":"name","data":"zg_list","name":"纸管","form_keys":{"pro_zg":"name"}},
				"pro_color":{"item":[],"index":0,"key":"name","data":"color_list","name":"颜色","form_keys":{"pro_color":"name"}},
				"pro_glue":{"item":[],"index":0,"key":"name","data":"glue_list","name":"胶水","form_keys":{"pro_glue":"name"}},
				"pro_bz":{"item":[],"index":0,"key":"name","data":"bz_list","name":"包装","form_keys":{"pro_bz":"name"}},
				"account_num":{"item":[],"index":0,"key":"name","data":"","name":"账套","form_keys":{"account_num":"value"}},
				"shop_id":{"item":[],"index":0,"key":"cStroreId","data":"","name":"门店","form_keys":{"shop_id":"cStroreId","shop_name":"cStoreName"}},
				"worker":{"item":[],"index":0,"key":"name","data":"","name":"贴膜师傅","form_keys":{"worker":"name"}},
				"store_type":{"item":[{"name":"中转仓","value":"中转仓"},{"name":"成品仓","value":"成品仓"}],"index":0,"key":"name","data":"","name":"仓库类型","form_keys":{"store_type":"name"}},
			},
			customerData:{},
			qrcodeUrl:"",
			specsShow:false,
			productInfo:this.product,
			popupShow:false,
			salerShow:false,
			salerInfo:{},
			boxShow:false,
			customerInfo:{},
			costInfo:{},
			costShow:false,
			fieldMap:api.fieldMap,
			images:[],
			member:uni.getStorageSync('MEMBER_INFO'),
			dateNow: new Date().Format("yyyy-MM-dd"),
			showSkip:false
		}
	},
	
	mounted() {
		_self = this;
		this.updateCustomerQrcode();
	},

	methods: {
		initData: async function(){
			this.boxShow = false;
			this.productInfo=this.product;
			this.formData["category"]=this.productInfo["pcategory"];
			this.formData["product_id"]=this.productInfo["id"];
			this.formData["roll_price"]=this.productInfo["roll_price"];
			this.formData["roll_weight"]=this.productInfo["roll_weight"];
			this.formData["product_type"]=this.productInfo["product_type"];
			
			this.formData["thick"]=this.productInfo["thick"];//默认厚度
			
			
			if(this.formInfo["inventory_code"]) this.formData["inventory_code"]=this.formInfo["inventory_code"];
	
			this.selectList["shop_id"]["item"]=await api.getShopList({});			
			this.selectList["store_type"]["item"]=await api.getUserStore({});
			
			let config=await api.getConfig({"key_name":['pro_zg_select','account_num','pro_cr_zg']});
		
			if(this.productInfo.pcategory=='OPP'){
				this.index=2;
			}
			if(this.productInfo.pcategory=='汽车膜'){
			
				this.selectList["worker"]["item"]=await api.getWorkerList({});
			}	
			
			if(this.productInfo.pcategory=='缠绕膜'){
				if(config["pro_cr_zg"]) this.productInfo["zg_list"]=config["pro_cr_zg"];
			}else{
				if(config["pro_zg_select"]) this.productInfo["zg_list"]=config["pro_zg_select"];
			}
			if(this.productInfo.pro_type!=1){	
				this.productInfo["zg_list"]=[];
			}	
			
			console.log(this.productInfo["zg_list"]);
					
			if(config["account_num"]) this.selectList["account_num"]["item"]=config["account_num"];
			console.log(this.formInfo);
			
			
			if(JSON.stringify(this.formInfo["customer"])=="[]") this.formInfo["customer"]={};
			
			if(this.formInfo["product_id"]){
				if(this.formInfo["customer"]) this.customerInfo=this.formInfo["customer"];//客户
				this.formData=this.formInfo;
			}
			
			
			
			if(this.formInfo["saler"]) this.salerInfo=this.formInfo["saler"];//销售人员
			if(this.formInfo["customer"]) this.customerInfo=this.formInfo["customer"];//销售人员
			
			
	
			
			
		    if(this.formInfo["shop_id"]){
				for(let index in this.selectList["shop_id"]["item"]){
					let item=this.selectList["shop_id"]["item"][index];
					if(item["cStroreId"]==this.formInfo["shop_id"]){
						this.selectList["shop_id"]["index"]=index;
						break;
					} 	
				}
			}
			
			
			
			for(let i in this.selectList){
				let item=this.selectList[i];
				if(item["data"]!=""&&this.productInfo[item["data"]].length>0) this.selectList[i]["item"]=this.productInfo[item["data"]];
				if(this.selectList[i]["item"].length>0){
					for(let k in this.selectList[i]["item"]){
						var item_k=this.selectList[i]["item"][k];
						if(item_k[item["key"]]==this.formData[i]) this.selectList[i]["index"]=k;
					}				
					
					if(this.selectList[i]["form_keys"]){
						for(let k1 in this.selectList[i]["form_keys"]){
							let item_k1=this.selectList[i]["form_keys"][k1];
							this.formData[k1]=this.selectList[i]["item"][this.selectList[i]["index"]][item_k1];
						}
					}
				}				
			}
			
			
			
			

			this.boxShow = true;
			this.specsShow=true;
			this.qrcodeUrl=this.host+'/ygxcg/Wechat/getQrcode?path=pages/get_user/index&user_id='+this.member.id+'&product_id='+this.product.id;
			
		},
		
		
		updateCustomerQrcode:async function(){
			let that=this;
			let intervals =this.$store.state.user.intervals
	
			let intervalId = setInterval(async function(){
				let customer=await api.getCustomerQrcode({"user_id":that.member.id,"product_id":that.product.id,"status":1});
				if(customer){
					that.customerInfo={
						"customer_name":customer["nickname"],
						"customer_kind":"周边小店",
						"address":"",
						"tel":customer["tel"]
					}
					clearInterval(intervalId);
				}
			},2000);　
			
			intervals.push(intervalId);
			this.$store.commit("setIntervals",intervals);
			　
		},
		
		
		computeData:function(){
			if(this.formData["box_num"]&&this.formData["piece_num"]){
				this.formData["roll_num"]=this.formData["box_num"]*this.formData["piece_num"];
				this.priceChange();
			}
		},
		
		change:function(e) {
			this.popupShow=e.show;
		},
		
		selectChange: function(e) {
			let key=e.currentTarget.dataset["key"];
		    this.selectList[key]["index"] = e.detail.value;
			
			if(this.selectList[key]["form_keys"]){
				for(let k1 in this.selectList[key]["form_keys"]){
					let item_k1=this.selectList[key]["form_keys"][k1];
					this.formData[k1]=this.selectList[key]["item"][this.selectList[key]["index"]][item_k1];
				}
			}
			
			

		},
		
		popupOpt:function(show){
			if(this.popupShow){
				this.$refs.popup.open('bottom');
			}else{
				this.$refs.popup.close();
			}
		},
		
		submitForm: async function(form){
			this.fieldVerify();
			
			if(this.errorField&&this.product["product_type"]!=2){
				uni.showToast({icon:"none",title:"请填写【"+this.fieldMap[this.errorField]+"】且是数字类型！"});
				return false;
			}
			
			
			if(this.formData["store_type"]=="成品仓"){
				
				this.isConfirm=false;
				this.showTip=true;
				this.showTxt="仓库类型："+this.formData["store_type"];
			}else{
				this.confirmSubmit(true);
			}
				
		},
		
		confirmSubmit: async function(val){
			if(!val) return false;
			let that=this;
			if(this.product["pro_type"]==2){
				this.formData["customer_kind"]="周边小店";
			}else{
				this.formData["customer_kind"]=this.customerInfo["customer_kind"];
			}
			this.formData["order_item_id"]=this.orderItemId;
			this.formData["customer"]=this.customerInfo;
			
			this.formData["saler"]=this.salerInfo;
			this.formData["print_imgs"]=[];
			if(this.images.length>0){
				for(let index in this.images){
					let item=this.images[index];
					this.formData["print_imgs"].push(item);
				}
			}
			
			let retInfo= await api.addOrder(this.formData);
			if(retInfo=="true"){
				uni.showToast({icon:"none",title:"添加成功！"});
				setTimeout(function(){that.showSkip=true;},1500);　

			}

		},
		
		formSkip:function(opt){
			switch(opt){
				case 1:
					uni.navigateTo({url: "/packagePages/pages/order/index?status=1"});	
				break;
				case 2:
					uni.switchTab({url: "/pages/product/index"});
				break;
			}
		},
		
		fieldVerify:function(){
			let exp = /^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/;
			_self.errorField="";			
			let fieldFilter=[];
			
			fieldFilter=["store_type","inventory_code","specs_index","order_item_id","pro_glue","print_imgs","print_txt","pro_bz","shop_id","shop_name","saler","customer_kind","thick","category","customer","product_id","roll_weight","account_num","piece_num","pro_zg","pro_zx","mark","roll_price","pro_color"];
			if(this.productInfo.pcategory=='缠绕膜'){
				fieldFilter.push("width");
				fieldFilter.push("length");
				fieldFilter.push("cr_weight");
			}
			if(this.productInfo.pcategory=='汽车膜'){
				fieldFilter.push("worker");

			}

			for(let index in _self.formData){
				if (fieldFilter.indexOf(index)!=-1) continue;
				if(!exp.test(_self.formData[index])||_self.formData[index]==""||_self.formData[index]<=0){
					
					_self.errorField=index;
				}
			}
		},
		
		computePrice:async function(){
			if(!this.customerInfo["customer_name"]&&this.product["pro_type"]==1){
				uni.showToast({icon:"none",title:"请选择客户！"});
				return false;
			}
			
			if(this.product["pro_type"]==2){
				this.formData["customer_kind"]="周边小店";
			}else{
				this.formData["customer_kind"]=this.customerInfo["customer_kind"];
			}
			
			//this.costShow=false;
			this.costInfo = await api.priceCost(this.formData);
			if(this.costInfo) this.costShow=true;
		},
		
		
		priceChange:function(){
			let exp = /^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/; 
			if(!this.formData.price||!this.formData.roll_num) return false;
			this.formData["total"]=Number(this.formData.price)*Number(this.formData.roll_num);
			this.formData.box_price=(Number(this.formData.price)*Number(this.formData.piece_num)).toFixed(2);
		},
		
		
		boxPriceChange:function(){
			let exp = /^[+-]?\d*(\.\d*)?(e[+-]?\d+)?$/;
			if(!this.formData.box_price||!this.formData.roll_num) return false;
			this.formData["total"]=Number(this.formData.box_price)*Number(this.formData.box_num);
			this.formData.price=(Number(this.formData.total)/Number(this.formData.roll_num)).toFixed(2);
		},
		
		
		totalChange:function(){
			this.formData.price=(Number(this.formData.total)/Number(this.formData.roll_num)).toFixed(2);
			this.formData.box_price=(Number(this.formData.total)/Number(this.formData.box_num)).toFixed(2);
		},
		
		realTotal:function(){
			this.formData.total=(this.formData.price*Number(this.formData.roll_num)).toFixed(2);
		},
		
		
		
		selectCustomer:async function(){
			this.popupShow=true;
		},
		
		selectSaler:async function(){
			this.salerShow=true;
		},
		
		goPage:function(e){
			uni.navigateTo({url: e.currentTarget.dataset.url});
		},
		
		showCode:function(){
			this.customerInfo={};
		}
	
	},
}